package com.fare.taluo.ftp;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * FTP配置属性统一管理
 * <p>负责将指定.properties属性文件中的ftp属性装载入<link>#FtpConfiguration</link>类
 * @author xiangsf 2012-12-6
 *
 */
public class FtpConfigurationManager {

	private static final String config_file = "ftp.properties";
	private Logger logger = Logger.getLogger(getClass());
	private static final String FTP_HOSTS = "ftp.hosts";
	
	private boolean hasLoad = false;
	
	private static FtpConfigurationManager _instance = new FtpConfigurationManager();
	//使用Map集合对象，支持多个FTP服务主机的ip配置
	private Map<String,FtpConfiguration> ftpConfigurations;
	private String[] ftpHosts;
	private FtpConfigurationManager(){}
	
	public static FtpConfigurationManager getInstance(){
		return _instance;
	}

	public Map<String, FtpConfiguration> getFtpConfigurations() throws ConfigurationException {
		if(!hasLoad){
			this.load();
		}
		return ftpConfigurations;
	}
	/**
	 * 从外部添加FTP服务提供者配置
	 * @param ftpHost 主机IP
	 * @param ftpPort 端口
	 * @param username 用户名
	 * @param password 密码
	 * @param remotedir 远程目录路径
	 */
	public void addConfiguration(String ftpHost, int ftpPort, String username,
			String password, String localdir, String remotedir,String remoteCharset, String localCharset){
		if(ftpConfigurations == null){
			ftpConfigurations = new HashMap<String, FtpConfiguration>();
		}
		ftpConfigurations.put(ftpHost, new FtpConfiguration(ftpHost, ftpPort, username,password, localdir ,
				remotedir, remoteCharset,  localCharset));
		hasLoad = true;
	}
	/**
	 * 根据提供的FTP服务IP地址找到对应的配置项
	 * <p>当没有初始化加载时，会自动进行一次加载
	 * @param ftpHost
	 * @return
	 * @throws ConfigurationException
	 */
	public FtpConfiguration getFtpConfigurationWithIp(String ftpHost) throws ConfigurationException{
		if(StringUtils.isNotEmpty(ftpHost)){
			return this.getFtpConfigurations().get(ftpHost);
		}
		return null;
	}
	/**
	 * 初始化加载配置文件
	 * @throws ConfigurationException
	 */
	public void load() throws ConfigurationException{
		if(logger.isDebugEnabled()){
			logger.debug("开始加载FTP配置文件....");
		}
		if(!hasLoad){
			ftpConfigurations = new HashMap<String, FtpConfiguration>();
			PropertiesConfiguration pc= new PropertiesConfiguration(config_file);
			//分隔FTP主机IP配置项,得到多个FTP服务提供者IP地址
			ftpHosts = pc.getStringArray(FTP_HOSTS);
			FtpConfiguration config = null;
			if(ftpHosts != null && ftpHosts.length > 0){
				for(String ip : ftpHosts){
					config = new FtpConfiguration(ip, pc);
					if(logger.isDebugEnabled()){
						logger.debug(String.format("FTP服务配置:%s", config.toString()));
					}
					ftpConfigurations.put(ip, config);
				}
			}
			hasLoad = true;
		}
	}
	public void reload() throws ConfigurationException{
		hasLoad = false;
		this.destroy();
		this.load();
	}
	
	/**
	 * 销毁,清空数据
	 */
	public void destroy(){
		ftpConfigurations = null;
		hasLoad = false;
	}
	
}
